﻿using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Cms.Core.Application.IService.UserInfo;
using Cms.Core.AspNetMvc.Mvc;
using Cms.Core.Infrastructure;
using Cms.Core.Infrastructure.Attributes;
using Cms.Core.Infrastructure.AuthHelper;
using Cms.Core.Infrastructure.Common;
using Cms.Core.Infrastructure.Ui;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Mvc;
using Cms.Core.Infrastructure.Extension;
using Cms.Core.Application.Service.UserInfo.Dto;
using Newtonsoft.Json.Linq;
using Cms.Core.Infrastructure.Helps;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using System.Threading;
using Cms.Core.AspNetMvc.Mvc.Filter;
using Cms.Core.Validation.Abstractions;

namespace Cms.Core.Web.Controllers
{

    /// <summary>
    /// 登录
    /// </summary>
    [Display("登录")]
   
   //[AllowAnonymous]
    //[Produces("application/json")]
    public class LoginController : WebApiController
    {

        private readonly IUserService _userService = null;
        private readonly PermissionRequirement _requirement;
        private readonly  IHttpContextAccessor _httpContextAccessor;


        public LoginController(IUserService userService, PermissionRequirement requirement, IHttpContextAccessor httpContextAccessor)
        {
            _userService = userService;
            _requirement = requirement;
            _httpContextAccessor = httpContextAccessor;
        }



        /// <summary>
        /// 异步登录得到令牌方法
        /// </summary>
        /// <param name="name"></param>
        /// <param name="paw"></param>
        /// <returns></returns>
        [Display("异步登录得到令牌方法")]
        [HttpPost]
        [DisableAuditing]

    
        public async Task<AjaxResult> LoginAsync([FromBody] LoginDto dto)
        {


            if (!ModelState.IsValid)
            {
                return new AjaxResult(ModelState.Values.SelectMany(o=>o.Errors).Select(o=>o.ErrorMessage).ToJoin(),AjaxResultType.Error);
            }
            var result = await _userService.VerificationLoginInfo(dto.Name, dto.Paw);


            return result.ToAjaxResult();


        }



        /// <summary>
        /// 刷新令牌
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        //[Display("刷新令牌")]
        //[HttpPost]
        //[AllowAnonymous]
        //public async Task<AjaxResult> RefreshToken([FromBody] JObject token)
        //{
        //    //token.NotNullOrEmpty(nameof(token));
        //    var model = JwtToken.AnalysisJwtToken(token.ToObject("token").AsTo<string>());
        //    if (model.Uid != null)
        //    {
        //        var userId = (string)model.Uid;
        //        var result = await _userService.RefreshToken(userId.AsTo<Guid>());
        //        return result.ToAjaxResult();
        //    }
        //    return new AjaxResult
        //    {
        //        Data = null,
        //        Message = "认证失败",
        //        Success = false,
        //        Type = AjaxResultType.Info,
        //    };
        //}
    }
}